package com.tt.jshb.index;

import android.content.Intent;
import android.support.v4.widget.SwipeRefreshLayout;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.arouter.launcher.ARouter;
import com.hzecool.app.bean.app.ARouterUrl;
import com.hzecool.common.utils.DeviceUtils;
import com.hzecool.common.utils.HandlerUtil;
import com.hzecool.common.utils.ResourceUtils;
import com.hzecool.common.utils.SPUtils;
import com.hzecool.common.utils.ToastUtils;
import com.hzecool.core.base.TBaseActivity;
import com.hzecool.core.base.TBaseFragment;
import com.hzecool.widget.materialdialog.MaterialDialog;
import com.tt.jshb.R;
import com.tt.jshb.account.login.LoginActivity;
import com.tt.jshb.app.Constants;
import com.tt.jshb.auth.main.AuthActivity;
import com.tt.jshb.bean.AuthStatusBean;
import com.tt.jshb.bean.IndexBanner;
import com.tt.jshb.bean.PhonePriceBean;
import com.tt.jshb.bean.ZigeBean;
import com.tt.jshb.mine.guide.HelpActivity;
import com.tt.jshb.recyclerList.RecyclerListActivity;
import com.tt.jshb.svc.GlideImageLoader;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author tutu
 * @time 2018/6/3
 */

public class IndexFragment extends TBaseFragment<IIndexView, IndexPresenter>
        implements IIndexView {
    @BindView(R.id.tv_sale)
    TextView tvSale;
    @BindView(R.id.srf)
    SwipeRefreshLayout swipeRefreshLayout;
    @BindView(R.id.tv_ring)
    View ring;
    @BindView(R.id.banner)
    Banner banner;

    private PhonePriceBean phonePriceBean;
    private List<String> imgs = new ArrayList<>();
    private IndexBanner bannerData;
    private AuthStatusBean authStatusBean;
    private ZigeBean zigeBean;

    @Override
    public void onLoadData(Object o) {

    }

    @Override
    public void onEmptyData() {

    }

    @Override
    public void onLoadError(String msg) {
        ToastUtils.showShortToast(msg);
        swipeRefreshLayout.setRefreshing(false);
    }

    @Override
    public void onNetError(String msg) {

    }

    @Override
    public int getLayoutID() {
        return R.layout.index_fragment_layout;
    }

    @Override
    public void initView() {
        mPresenter.getPhonePrice(DeviceUtils.getModel(), DeviceUtils.getTotalInternalMemorySize());

        swipeRefreshLayout.setOnRefreshListener(() -> {
            mPresenter.getBanner();
            mPresenter.zige();
            mPresenter.authStatus();

            HandlerUtil.postDelay(() -> {
                if (swipeRefreshLayout != null) {
                    swipeRefreshLayout.setRefreshing(false);
                }
            }, 3000);
        });


        ring.setOnClickListener(view -> {
            Intent intent = new Intent(getActivity(), HelpActivity.class);
            startActivity(intent);
        });

        banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR_TITLE);
        banner.setDelayTime(3000);
    }

    @Override
    public void initTitle(ImageView ivBack, TextView tvBack, View llBack, TextView titleName, TextView tvMenu, View titleRoot) {
    }

    @Override
    protected IndexPresenter createPresenter() {
        return new IndexPresenter();
    }


    @OnClick({R.id.tv_sale})
    public void onClick(View view) {

        String tokens = SPUtils.getString(Constants.SP_TOKENDS);
        if (TextUtils.isEmpty(tokens)) {
            Intent intent = new Intent(getActivity(), LoginActivity.class);
            startActivity(intent);
            getActivity().finish();
            ToastUtils.showShortToast("请先登录");
            return;
        }

        switch (view.getId()) {
            case R.id.tv_sale:
                if ("1".equals(this.phonePriceBean.getData().getMark())) {
                    new MaterialDialog.Builder(getActivity())
                            .title("提示")
                            .content("系统正在升级维护")
                            .positiveText(ResourceUtils.getString(R.string.confirm))
                            .show();
                    return;
                } else {
                    toSale();
                }
                break;
            default:
                break;
        }
    }

    private void toSale() {
        if ("1".equals(this.zigeBean.getData().getCloseMark())) {
            ((TBaseActivity)mActivityContext).showAlertDlg("提示", "您暂时无法下单");

            return;
        }


        if (authStatusBean != null) {
            if (authStatusBean.getData().needAuth()) {
                /**
                 * 需要继续认证
                 */
                Intent intent1 = new Intent(mActivityContext, AuthActivity.class);
                intent1.putExtra("auth", this.authStatusBean);
                startActivity(intent1);
            } else {
                /**
                 * 已经认证过了
                 */
                Intent intent1 = new Intent(mActivityContext, RecyclerListActivity.class);
                startActivity(intent1);
            }
        } else {
            ToastUtils.showShortToast("请求失败");
        }
    }

    @Override
    public void BannerOk(IndexBanner indexBanner) {
        imgs.clear();
        for (IndexBanner.DataBean dataBean : indexBanner.getData()) {
            imgs.add(dataBean.getImg());
        }
        bannerData = indexBanner;
        swipeRefreshLayout.setRefreshing(false);
        banner.setOnBannerListener(position -> ARouter.getInstance().build(ARouterUrl.AR_URL_WEB_VIEW)
                .withString("url", bannerData.getData().get(position).getUrl()).navigation());
        //设置图片加载器
        banner.setImageLoader(new GlideImageLoader());
        //设置图片集合
        banner.setImages(imgs);
        //banner设置方法全部调用完毕时最后调用
        banner.start();
    }

    @Override
    public void PhonePriceOk(PhonePriceBean rep) {
        this.phonePriceBean = rep;
        swipeRefreshLayout.setRefreshing(false);
    }

    @Override
    public void PhonePriceFail(PhonePriceBean rep) {
        tvSale.setEnabled(false);
        swipeRefreshLayout.setRefreshing(false);
        ToastUtils.showShortToast(rep.getMsg());
    }

    @Override
    public void onAuthStatusOk(AuthStatusBean bean) {
        this.authStatusBean = bean;
    }

    @Override
    public void onZige(ZigeBean zigeBean) {
        this.zigeBean = zigeBean;
    }


    //如果你需要考虑更好的体验，可以这么操作
    @Override
    public void onStart() {
        super.onStart();
    }

    @Override
    public void onStop() {
        super.onStop();
    }

}

